package pers.xx.sbm.service.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import pers.xx.sbm.dao.UserMapper;
import pers.xx.sbm.dao.UserRoleMapper;
import pers.xx.sbm.entity.User;
import pers.xx.sbm.entity.UserRole;
import pers.xx.sbm.enums.ResultEnum;
import pers.xx.sbm.service.UserService;
import pers.xx.sbm.utils.Md5Utils;
import pers.xx.sbm.utils.UserUtils;
import pers.xx.sbm.vo.ResResult;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：xiexing
 * @description：用户操作实现类
 * @date ：2020/8/16 11:44
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

    UserMapper userMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    private UserUtils userUtils;//用户工具

    public UserServiceImpl(@Autowired UserMapper userMapper) {
        super.dao = userMapper;
        this.userMapper = userMapper;
    }

    /**
     * @param userName 用户名
     * @description 根据用户名查找用户
     * @author XieXing
     * @date 2020/9/20 18:08
     */
    @Override
    public User findByUserName(String userName) {
        return userMapper.findByUserName(userName);
    }

    /**
     * @param userPhone 用户手机号
     * @description 根据用户手机号查找用户
     * @author XieXing
     * @date 2020/10/15 14:04
     */
    @Override
    public User findByUserPhone(String userPhone) {
        return userMapper.findByUserPhone(userPhone);
    }

    /**
     * @description 插入用户及用户角色
     * @author XieXing
     * @date 2020/10/12 17:22
     */
    @Transactional
    @Override
    public ResResult insertWithRoles(User user, String roles) {
        User currentUser = userUtils.getCurrentUser();
        Date addTime = new Date();
        user.setUserAddTime(addTime);
        user.setUserAddby(currentUser.getUserName());
        user.setUserAddbyId(currentUser.getId());
        user.setUserIsDel(0);//默认未删除
        user.setUserType(1);
        user.setUserIsEnabled(1);//默认可用状态
        user.setUserPassword(Md5Utils.getMd5Str(user.getUserPassword()));
        userMapper.insert(user);
        int userId = user.getId();//获取自增id
        setUserRoles(userId, currentUser, roles);
        return new ResResult(ResultEnum.SUCCESS, "用户信息保存成功");
    }

    /**
     * @description 修改用户信息 并修改用户角色
     * @author XieXing
     * @date 2020/10/15 10:11
     */
    @Transactional
    @Override
    public ResResult updateWithRoles(User user, String roles) {
        User currentUser = userUtils.getCurrentUser();
        user.setUserUpdatebyId(currentUser.getId());
        user.setUserUpdateby(currentUser.getUserName());
        user.setUserUpdateTime(new Date());
        //密码为空 则不修改原来的密码
        if (!StringUtils.isEmpty(user.getUserPassword())) {
            user.setUserPassword(Md5Utils.getMd5Str(user.getUserPassword()));
        } else {
            user.setUserPassword(null);
        }
        userMapper.updateByPrimaryKeySelective(user);
        userRoleMapper.deleteByUserId(user.getId());
        setUserRoles(user.getId(), currentUser, roles);
        return new ResResult(ResultEnum.SUCCESS, "用户信息更新成功");
    }


    /**
     * @description 设置用户角色信息
     * @author XieXing
     * @date 2020/10/15 10:11
     */
    private int setUserRoles(Integer userId, User currentUser, String roles) {
        //角色为空就不插入角色
        if (StringUtils.isEmpty(roles)) {
            return 0;
        }
        String[] roleArr = roles.split(",");
        List<UserRole> userRoles = new ArrayList<>();
        for (int i = 0; i < roleArr.length; i++) {
            if (!StringUtils.isEmpty(roleArr[i])) {
                UserRole userRole = new UserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(Integer.valueOf(roleArr[i]));
                userRole.setUserRoleAddby(currentUser.getUserName());
                userRole.setUserRoleAddbyId(currentUser.getId());
                userRole.setUserRoleAddTime(new Date());
                userRoles.add(userRole);
            }
        }
        return userRoleMapper.insertBatch(userRoles);
    }
}
